//
//  NSDate+Formatter.h
//  Stoneobs
//
//  Created by Stoneobs on 16/7/30.
//  Copyright © 2016年 Stoneobs. All rights reserved.
//

#import "NSDate+STTools_Formatter.h"

@implementation NSDate (STTools_Formatter)
//获取此时的本地时间
+ (NSDate*)stoneobs_fun_localCurrentDate
{
    NSDate *stoneobs_temp_date = [NSDate date];
    NSTimeZone *stoneobs_temp_zone = [NSTimeZone systemTimeZone];
    NSInteger stoneobs_temp_interval = [stoneobs_temp_zone secondsFromGMTForDate: stoneobs_temp_date];
    NSDate *stoneobs_temp_localeDate = [stoneobs_temp_date  dateByAddingTimeInterval: stoneobs_temp_interval];
    return stoneobs_temp_localeDate;
}
+ (NSString *)st_currentDateStringWithFormat:(NSString *)format
{
    NSDate *stoneobs_temp_chosenDate = [NSDate date];
    NSDateFormatter *stoneobs_temp_formatter = [[NSDateFormatter alloc] init];
    [stoneobs_temp_formatter setDateFormat:format];
    NSString *stoneobs_temp_date = [stoneobs_temp_formatter stringFromDate:stoneobs_temp_chosenDate];
    return stoneobs_temp_date;
}
+ (NSDate *)st_dateWithSecondsFromNow:(NSInteger)seconds {
    NSDate *stoneobs_temp_date = [NSDate date];
    NSDateComponents *stoneobs_temp_components = [NSDateComponents new];
    [stoneobs_temp_components setSecond:seconds];
    NSCalendar *stoneobs_temp_calendar = [NSCalendar currentCalendar];
    NSDate *stoneobs_temp_dateSecondsAgo = [stoneobs_temp_calendar dateByAddingComponents:stoneobs_temp_components toDate:stoneobs_temp_date options:0];
    return stoneobs_temp_dateSecondsAgo;
}
+ (NSDate *)st_dateWithYear:(NSInteger)year month:(NSUInteger)month day:(NSUInteger)day {
    NSCalendar *stoneobs_temp_calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *stoneobs_temp_components = [[NSDateComponents alloc] init];
    [stoneobs_temp_components setYear:year];
    [stoneobs_temp_components setMonth:month];
    [stoneobs_temp_components setDay:day];
    return [stoneobs_temp_calendar dateFromComponents:stoneobs_temp_components];
}
//获取某个date 的本地时间
- (NSDate*)stoneobs_fun_localDate
{
    NSDate *stoneobs_temp_date = self;
    NSTimeZone *stoneobs_temp_zone = [NSTimeZone systemTimeZone];
    NSInteger stoneobs_temp_interval = [stoneobs_temp_zone secondsFromGMTForDate: stoneobs_temp_date];
    NSDate *stoneobs_temp_localeDate = [stoneobs_temp_date  dateByAddingTimeInterval: stoneobs_temp_interval];
    return stoneobs_temp_localeDate;
}
- (NSString *)st_dateWithFormat:(NSString *)format
{
    NSDateFormatter *stoneobs_temp_formatter = [[NSDateFormatter alloc] init];
    [stoneobs_temp_formatter setDateFormat:format];
    NSString *stoneobs_temp_date = [stoneobs_temp_formatter stringFromDate:self];
    return stoneobs_temp_date;
}
- (NSString *)stoneobs_fun_yyyyMMByLineWithDate{
    NSDateFormatter *stoneobs_temp_formatter = [[NSDateFormatter alloc] init];
    [stoneobs_temp_formatter setDateFormat:@"yyyy-MM"];
    return [stoneobs_temp_formatter stringFromDate:self];
}
- (NSString *)stoneobs_fun_mmddByLineWithDate{
    NSDateFormatter *stoneobs_temp_formatter = [[NSDateFormatter alloc] init];
    [stoneobs_temp_formatter setDateFormat:@"MM-dd"];
    return [stoneobs_temp_formatter stringFromDate:self];
}

- (NSString *)stoneobs_fun_mmddChineseWithDate{
    NSDateFormatter *stoneobs_temp_formatter = [[NSDateFormatter alloc] init];
    [stoneobs_temp_formatter setDateFormat:@"MM月dd日"];
    return [stoneobs_temp_formatter stringFromDate:self];
}

- (NSString *)stoneobs_fun_hhmmssWithDate{
    NSDateFormatter *stoneobs_temp_formatter = [[NSDateFormatter alloc] init];
    stoneobs_temp_formatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
    [stoneobs_temp_formatter setDateFormat:@"HH:mm:ss"];
    return [stoneobs_temp_formatter stringFromDate:self];
}

- (NSString *)stoneobs_fun_yyyyMMddByLineWithDate{
    NSDateFormatter *stoneobs_temp_formatter = [[NSDateFormatter alloc] init];
    [stoneobs_temp_formatter setDateFormat:@"yyyy-MM-dd"];
     NSTimeZone* stoneobs_temp_GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    [stoneobs_temp_formatter setTimeZone:stoneobs_temp_GTMzone];
    return [stoneobs_temp_formatter stringFromDate:self];
}
+ (NSDate *)stoneobs_fun_dateWithYyyyMMDDByLineString:(NSString*)string{
    NSDateFormatter *stoneobs_temp_formatter = [[NSDateFormatter alloc] init];
    [stoneobs_temp_formatter setDateFormat:@"yyyy-MM-dd"];
     NSTimeZone* stoneobs_temp_GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    [stoneobs_temp_formatter setTimeZone:stoneobs_temp_GTMzone];
    NSDate * stoneobs_temp_date = [stoneobs_temp_formatter dateFromString:string];
    return stoneobs_temp_date;
}
- (NSString *)stoneobs_fun_yyyyMMddhhmmssWithDate
{
    NSDateFormatter *stoneobs_temp_formatter = [[NSDateFormatter alloc] init];
    [stoneobs_temp_formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone* stoneobs_temp_GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    [stoneobs_temp_formatter setTimeZone:stoneobs_temp_GTMzone];
    return [stoneobs_temp_formatter stringFromDate:self];
}
- (NSString *)stoneobs_fun_morningOrAfterWithHH{
    NSDateFormatter *stoneobs_temp_formatter = [[NSDateFormatter alloc] init];
    [stoneobs_temp_formatter setDateFormat:@"HH"];
    NSString *stoneobs_temp_status = [stoneobs_temp_formatter stringFromDate:self];
    if (stoneobs_temp_status.intValue > 0 && stoneobs_temp_status.intValue < 12) {
        return @"上午好";
    }else{
        return @"下午好";
    }
    return @"";
}
-(NSString *)stoneobs_fun_weekOrMonth
{
    //[NSDate dateWithTimeIntervalSince1970:time.integerValue/1000]
    NSDate * stoneobs_temp_date = self;
    NSCalendar *stoneobs_temp_greCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    [stoneobs_temp_greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDateComponents* com= [stoneobs_temp_greCalendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitWeekOfYear |NSCalendarUnitWeekday fromDate:stoneobs_temp_date];
    
    NSCalendar *curentCal = [NSCalendar currentCalendar];
    [stoneobs_temp_greCalendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    NSDateComponents* curentCalcom= [curentCal components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitWeekOfYear|NSCalendarUnitWeekday fromDate:[NSDate date]];
    NSString * hourMin=[NSString stringWithFormat:@"%lu:%lu:%lu",com.hour,com.minute,com.second];
    BOOL isThisYear =(com.year==curentCalcom.year);
    if (isThisYear&&curentCalcom.weekOfYear==com.weekOfYear) {
        return [NSString stringWithFormat:@"周%@%@",[self st_weekDay:com.weekday],hourMin];
    }
    if (isThisYear&&com.month==curentCalcom.month) {
        return [NSString stringWithFormat:@"本月%lu日%lu时",com.day,com.hour];
    }
    return [NSString stringWithFormat:@"%lu年%lu月%lu日 %lu:%lu:%lu:",com.year,com.month,com.day,
            com.hour,com.minute,com.second];

}
-(NSString*)st_weekDay:(NSInteger)num
{
    switch (num) {
        case 1:
            return @"日";
            break;
        case 2:
            return @"一";
            break;
        case 3:
            return @"二";
            break;
        case 4:
            return @"三";
            break;
        case 5:
            return @"四";
            break;
        case 6:
            return @"五";
            break;
        case 7:
            return @"六";
            break;
        default:
            break;
    }
    return nil;
}
- (NSString *)stoneobs_fun_ynamicFormater{
    NSInteger stoneobs_temp_time = [self timeIntervalSince1970];
    NSDate * stoneobs_temp_currentDate = [NSDate stoneobs_fun_localCurrentDate];
    NSInteger stoneobs_temp_now = [stoneobs_temp_currentDate timeIntervalSince1970];
    NSInteger chazhi = stoneobs_temp_now - stoneobs_temp_time;
    if (chazhi <  60 * 3) {
        return @"刚刚";
    }else if (chazhi < 60 * 60  ){
        NSInteger min = chazhi / 60;
        NSString * desc =  [NSString stringWithFormat:@"%lu分钟前",min];
        return desc;
    }else if (chazhi < 60 * 60 * 24  ){
        NSInteger hour = chazhi / 3600;
        NSString * desc =  [NSString stringWithFormat:@"%lu小时前",hour];
        return desc;
    }else if (chazhi < 60 * 60 * 24  * 2  ){
        NSString * desc =  @"一天前";
        return desc;
    }else if (chazhi < 60 * 60 * 24  * 30  ){
        NSInteger day = chazhi / (3600*24);
        NSString * desc = [NSString stringWithFormat:@"%lu天前",day];
        return desc;
    }
    return self.stoneobs_fun_yyyyMMddhhmmssWithDate;
}
+ (NSString *)stoneobs_fun_countDownWithSecend:(NSInteger)secend{
    NSInteger stoneobs_temp_hour = secend / 3600;
    NSString * stoneobs_temp_hourStr = @(stoneobs_temp_hour).description;
    if (stoneobs_temp_hourStr.length < 2) {
        stoneobs_temp_hourStr = [NSString stringWithFormat:@"0%@",@(stoneobs_temp_hour)];
    }
    
    NSInteger stoneobs_temp_minte = (secend - stoneobs_temp_hour*3600) / 60;
    NSString * stoneobs_temp_minteStr = @(stoneobs_temp_minte).description;
    if (stoneobs_temp_minteStr.length < 2) {
        stoneobs_temp_minteStr = [NSString stringWithFormat:@"0%@",@(stoneobs_temp_minte)];
    }
    
    NSInteger stoneobs_temp_sec = secend % 60;
    NSString * stoneobs_temp_secStr = @(stoneobs_temp_sec).description;
    if (stoneobs_temp_secStr.length < 2) {
        stoneobs_temp_secStr = [NSString stringWithFormat:@"0%@",@(stoneobs_temp_sec)];
    }
    return [NSString stringWithFormat:@"%@:%@:%@",stoneobs_temp_hourStr,stoneobs_temp_minteStr,stoneobs_temp_secStr];
}

//2个日期 差距几天
+ (NSInteger)stoneobs_fun_countFormDate:(NSDate*)date endDate:(NSDate*)enddate{
    NSString * stoneobs_temp_string = enddate.stoneobs_fun_yyyyMMddByLineWithDate;
    NSString * stoneobs_temp_endString = [NSString stringWithFormat:@"%@ 23:59:59",stoneobs_temp_string];
    
    NSString * stoneobs_temp_bstring = date.stoneobs_fun_yyyyMMddByLineWithDate;
    NSString * stoneobs_temp_beginString = [NSString stringWithFormat:@"%@ 23:59:59",stoneobs_temp_bstring];
    
    NSDateFormatter *stoneobs_temp_formatter = [[NSDateFormatter alloc] init];
    [stoneobs_temp_formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone* stoneobs_temp_GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    [stoneobs_temp_formatter setTimeZone:stoneobs_temp_GTMzone];
    
    NSDate * stoneobs_temp_dealDate = [stoneobs_temp_formatter dateFromString:stoneobs_temp_endString];
    NSDate * stoneobs_temp_dealBegin = [stoneobs_temp_formatter dateFromString:stoneobs_temp_beginString];
    
    NSInteger stoneobs_temp_end = [stoneobs_temp_dealDate timeIntervalSince1970];
    NSInteger stoneobs_temp_begin = [stoneobs_temp_dealBegin timeIntervalSince1970];
    
    NSInteger count = (stoneobs_temp_end - stoneobs_temp_begin) / (24*60*60);
    return count;
    
}
- (NSDate*)stoneobs_fun_to_23_59_59date{
    NSString * stoneobs_temp_string = self.stoneobs_fun_yyyyMMddByLineWithDate;
    NSString * stoneobs_temp_endString = [NSString stringWithFormat:@"%@ 23:59:59",stoneobs_temp_string];
    
    NSDateFormatter *stoneobs_temp_formatter = [[NSDateFormatter alloc] init];
    [stoneobs_temp_formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone* stoneobs_temp_GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    [stoneobs_temp_formatter setTimeZone:stoneobs_temp_GTMzone];
    NSDate * stoneobs_temp_dealDate = [stoneobs_temp_formatter dateFromString:stoneobs_temp_endString];
    return stoneobs_temp_dealDate;
}
- (NSDate*)stoneobs_fun_to_to_00_00_00date{
    NSString * stoneobs_temp_string = self.stoneobs_fun_yyyyMMddByLineWithDate;
    NSString * stoneobs_temp_endString = [NSString stringWithFormat:@"%@ 00:00:00",stoneobs_temp_string];
    
    NSDateFormatter *stoneobs_temp_formatter = [[NSDateFormatter alloc] init];
    [stoneobs_temp_formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSTimeZone* stoneobs_temp_GTMzone = [NSTimeZone timeZoneForSecondsFromGMT:0];
    [stoneobs_temp_formatter setTimeZone:stoneobs_temp_GTMzone];
    NSDate * stoneobs_temp_dealDate = [stoneobs_temp_formatter dateFromString:stoneobs_temp_endString];
    return stoneobs_temp_dealDate;
}
@end
